/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.BookedTicket;

/**
 *
 * @author Varadha
 */
public class BookedTicketDAO extends BaseEntityDAO
{

    public BookedTicketDAO()
    {
        // initialization
    }

    /**
     * Insert bookedticket into database
     * @param bookedticket the new bookedticket entity to be inserted
     * @return newly created bookedticket number or a 0 on error
     */
    public int insertBookedTicket(BookedTicket bookedticket)
    {
        return save(bookedticket);
    }

    /**
     * Delete bookedticket that matches the supplied criteria
     * @param bookedticket the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteBookedTicket(BookedTicket bookedticket)
    {
        return delete(bookedticket);
    }

    /**
     * Update the bookedticket using data from the bookedticket Data Object
     * @param bookedticket the bookedticket Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateBookedTicket(BookedTicket bookedticket)
    {
        if (bookedticket.getCustomerID() == 0 || bookedticket.getEventID() == 0 || bookedticket.getSeatCategoryID()
                                                                                   == 0 || bookedticket.getVenueID() == 0)
        {
            return false;
        }

        if (update(bookedticket))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the bookedticket that matches the supplied criteria
     * @param bookedticket the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public BookedTicket findBookedTicket(BookedTicket bookedticket)
    {
        String sql = null;

        if (bookedticket.getCustomerID() != 0 && bookedticket.getEventID() != 0 && bookedticket.getSeatCategoryID()
                                                                                   != 0 && bookedticket.getVenueID() != 0)
        {
            sql = buildQuerySQL(bookedticket);
        }
        else
        {
            return null;
        }

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(BookedTicketDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (BookedTicket) this.getObject(crs, bookedticket);
    }

    /**
     * Get all bookedtickets that match the supplied criteria
     * @param bookedticket supplied criteria
     * @return a RowSet
     */
    public RowSet getBookedTicketsRS(BookedTicket bookedticket)
    {
        String sql = buildQuerySQL(bookedticket);
        return query(sql);
    }

    /**
     * Get all bookedtickets that match the supplied criteria
     * @param bookedticket supplied criteria
     * @return a collection of BookedTicket Data Objects
     */
    public Collection<BookedTicket> getBookedTicketsDO(BookedTicket bookedticket)
    {
        String sql = buildQuerySQL(bookedticket);

        RowSet crs = query(sql);
        Collection<BookedTicket> bookedtickets = new ArrayList<BookedTicket>();
        try
        {
            while (crs.next())
            {
                BookedTicket bookedticketDO = (BookedTicket) this.getObject(crs, bookedticket);
                bookedtickets.add(bookedticketDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(BookedTicketDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return bookedtickets;
    }

    /**
     * Get all bookedtickets that match the supplied criteria
     * @param bookedticket supplied criteria
     * @return a collection of BookedTicket Data Objects
     */
    public Collection<BookedTicket> getAvailableBookedTicketsDO(BookedTicket bookedticket)
    {
        //String sql = "select * from bookedticket where isCancelled = 1 or (isPaid = 0 and seatNumber not in (select seatNumber from bookedticket where bookedDate > '" + bookedticket.getBookedDate() + "'))";
        String sql = "select * from bookedticket where (bookedDate > '" + bookedticket.getBookedDate() + "' or isPaid = 1) and seatCategoryID = " + bookedticket.getSeatCategoryID();

        RowSet crs = query(sql);
        Collection<BookedTicket> bookedtickets = new ArrayList<BookedTicket>();
        try
        {
            while (crs.next())
            {
                BookedTicket bookedticketDO = (BookedTicket) this.getObject(crs, bookedticket);
                BookedTicket bookedTicket = new BookedTicket();
                bookedTicket.setBookedDate(bookedticketDO.getBookedDate());
                bookedTicket.setCustomerID(bookedticketDO.getCustomerID());
                bookedTicket.setEventID(bookedticketDO.getEventID());
                bookedTicket.setIsCancelled(bookedticketDO.isIsCancelled());
                bookedTicket.setIsPaid(bookedticketDO.isIsPaid());
                bookedTicket.setSeatCategoryID(bookedticketDO.getSeatCategoryID());
                bookedTicket.setSeatNumber(bookedticketDO.getSeatNumber());
                bookedTicket.setTransactionID(bookedticketDO.getTransactionID());
                bookedTicket.setVenueID(bookedticketDO.getVenueID());
                bookedtickets.add(bookedTicket);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(BookedTicketDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return bookedtickets;
    }

    public boolean updateBookedTicketStatus(BookedTicket bookedticket)
    {
        String sql = "update bookedticket set isCancelled = 1 where (bookedDate < '" + bookedticket.getBookedDate() + "' and isCancelled = 0 and isPaid = 0)";
        int result = execute(sql, DataOperationType.Update);
        if (result == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
